package cn.edu.scau.cmi.linjinyong.client;

import java.io.File;

import cn.edu.scau.cmi.linjinyong.consistentComposite.Organization;
import cn.edu.scau.cmi.linjinyong.consistentComposite.Person;
import cn.edu.scau.cmi.linjinyong.consistentComposite.Team;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class ConsistentCompositeClient extends Application {

    private TreeView treeView = new TreeView();
    private TreeItem root;
    private Organization addedOrganization;

    private Image personImage;
    private Image teamImage;

    private TreeItem<Organization> choosedItem;


    private Button addPerson = new Button("创建个人");
    private Button addTeam = new Button("创建团队");
    private Button deleteOrganization = new Button("删除");
    private Button reset = new Button("重置");

    @Override
    public void start(Stage primaryStage) throws Exception {
        initialize();
        AnchorPane root = new AnchorPane();
        Label label = new Label("注意：除第一个组织外，必须选中个人/团队才能进行创建/删除操作");
        label.setFont(Font.font("楷体", 18));
        label.setLayoutX(40);
        label.setLayoutY(25);

        ObservableList<Node> children = root.getChildren();
        children.add(label);
        children.add(treeView);
        children.add(addPerson);
        children.add(addTeam);
        children.add(deleteOrganization);
        children.add(reset);

        primaryStage.setTitle("一致性组合模式");
        primaryStage.setScene(new Scene(root, 600, 500));
        primaryStage.show();
    }

    private void initialize() {
        this.personImage = new Image("file:./resources/person.jpg");
        this.teamImage = new Image("file:./resources/team.jpg");

        addPerson.setLayoutX(50);
        addPerson.setLayoutY(400);
        addPerson.setPrefSize(100, 40);
        addPerson.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                addedOrganization = new Person();
                display();
            }
        });

        addTeam.setLayoutX(180);
        addTeam.setLayoutY(400);
        addTeam.setPrefSize(100, 40);
        addTeam.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                addedOrganization = new Team();
                display();
            }
        });

        deleteOrganization.setLayoutX(310);
        deleteOrganization.setLayoutY(400);
        deleteOrganization.setPrefSize(100, 40);
        deleteOrganization.setDisable(true);
        deleteOrganization.setOnMouseClicked((MouseEvent) -> {
            removeTreeItem();
        });

        reset.setLayoutX(440);
        reset.setLayoutY(400);
        reset.setPrefSize(100, 40);
        reset.setDisable(true);
        reset.setOnMouseClicked((MouseEvent) -> {
            reset();
        });

        treeView.setPrefSize(500, 300);
        treeView.setLayoutX(50);
        treeView.setLayoutY(80);
        treeView.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                choosedItem = ((TreeItem<Organization>) treeView.getSelectionModel().getSelectedItem());
                if (choosedItem != null) {
                    deleteOrganization.setDisable(false);
                    if (choosedItem.getValue() instanceof Person) {
                        addPerson.setDisable(true);
                        addTeam.setDisable(true);
                    } else {
                        addPerson.setDisable(false);
                        addTeam.setDisable(false);
                    }
                }
            }
        });
    }

    private void addTreeItem(Organization o) {
        TreeItem<Organization> treeItem = new TreeItem<>(o);
        treeItem.setExpanded(true);
        ImageView icon = new ImageView();
        icon.setFitWidth(16);
        icon.setFitHeight(16);

        if (o instanceof Person) {
            icon.setImage(personImage);
        } else {
            icon.setImage(teamImage);
        }
        treeItem.setGraphic(icon);
        if (root == null) {
            root = treeItem;
            treeView.setRoot(root);
            addTeam.setDisable(true);
            addPerson.setDisable(true);
        } else {
            choosedItem.getChildren().add(treeItem);
            Team parentOrganization = (Team) choosedItem.getValue();
            parentOrganization.addTeammate(o);
        }
        this.reset.setDisable(false);
    }

    private void removeTreeItem() {
        if (choosedItem != null) {
            TreeItem parent = choosedItem.getParent();
            if (parent != null) {
                Team parentOrganization = (Team) parent.getValue();

                parentOrganization.removeTeammate((Organization) choosedItem.getValue());
                parent.getChildren().remove(choosedItem);
            } else {
                reset();
            }
        }
    }

    public void display() {
        Stage window = new Stage();
        window.setTitle("输入信息");
        //modality要使用Modality.APPLICATION_MODEL
        window.initModality(Modality.APPLICATION_MODAL);
        window.setMinWidth(300);
        window.setMinHeight(150);

        Label tip = new Label("输入名字：");
        TextField name = new TextField();
        name.setPrefHeight(30);

        Button button = new Button("完成");
        button.setOnAction(e -> {
            addedOrganization.setName(name.getText());
            addTreeItem(addedOrganization);
            window.close();
        });

        VBox layout = new VBox(10);
        layout.getChildren().addAll(tip, name, button);
        layout.setAlignment(Pos.CENTER);

        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.showAndWait();
    }

    private void reset(){
        this.root = null;
        this.treeView.setRoot(root);
        this.deleteOrganization.setDisable(true);
        this.reset.setDisable(true);
        this.addTeam.setDisable(false);
        this.addPerson.setDisable(false);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
